ls(all=TRUE)

args=(commandArgs(TRUE))
cat(args,"\n")

if(length(args)==0){
    print("No arguments supplied.")
    ##supply default values
    k = 1
}else{
    for(i in 1:length(args)){
         eval(parse(text=args[[i]]))
    }
}

cat(k," = Argument\n")

library("mecdf")
idrange=k*100

setwd("/ifs/scratch/c2b2/ip_lab/to2232/lk")
FILE_INPUT_LIST ="PopIndex.txt"
FILE_INPUT_LEN ="Lengths.txt"
FILE_OUTPUT_LK =paste("lk.cpp.",k,sep="")
FILE_OUTPUT_X ="x.pcn.txt"

idx<-scan(FILE_INPUT_LIST , list(id=""))
ibdlen<-read.table(FILE_INPUT_LEN)

x<-read.table(FILE_OUTPUT_X)
pcdf<-mecdf (as.matrix(x), continuous=FALSE, validate=TRUE,  project=FALSE, expandf=0.1)


threshold1=1000
threshold2=1000	

x$V1[which(x$V1<threshold1)]=0
x$V2[which(x$V2<threshold2)]=0

pcdf2<-mecdf (as.matrix(x), continuous=FALSE, validate=TRUE,  project=FALSE, expandf=0.1)




#plotbcdf(pcdf, regular=TRUE, res=128)

lkmatrix<-c()
for(i in 1:idrange)
{
	for(j in 1:length(idx$id))
	{
		for(k in 1:length(idx$id))
		{
		
		lk<-pcdf(matrix( c(ibdlen[i,j],ibdlen[i,k]), ncol=2))
		lkmatrix<-rbind(lkmatrix,list(id1=i,id2=j,id3=k,length=lk))
		}
	}
}

#4097
write.table(lkmatrix, file = FILE_OUTPUT_LK,col.names=F,row.names=F)


